package cn.com.xuhx.testService;

import cn.com.xuhx.pojo.User;
import cn.com.xuhx.service.UserService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * updateWrapper测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UpdateWrapperTest {

    @Autowired
    private UserService userService;

    /**
     * 根据指定条件修改指定字段
     */
    @Test
    public void updateWrapperTest(){
        //UPDATE T_USERS SET name=?,age=? WHERE delStatus=0 AND (id = ?)
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set("name","小魔仙小兰");
        wrapper.set("age",109);
        wrapper.eq("id",12L);
        userService.update(wrapper);
    }

    /**
     * 修改环境测试
     */
    @Test
    public void updateSDTest(){
        //UPDATE T_USERS SET name=?,age=? WHERE delStatus=0 AND (id = ?)
        String name = "魔仙堡太子妃小兰";
        Integer age = 110;
        Long id = 12L;
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set(StringUtils.isNotBlank(name),"name",name);
        wrapper.set(age != null,"age",age);
        wrapper.eq(id != null,"id",id);
        userService.update(wrapper);
    }


    /**
     * 修改环境测试
     */
    @Test
    public void updateSDByLambda(){
        //UPDATE T_USERS SET name=?,age=? WHERE delStatus=0 AND (id = ?)
        String name = "魔仙堡太子妃小兰";
        Integer age = 110;
        Long id = 12L;
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(StringUtils.isNotBlank(name),User::getName,name);
        wrapper.set(age != null,User::getAge,age);
        wrapper.eq(id != null,User::getId,id);
        userService.update(wrapper);
    }
}
